package net.mysgame.czfun.utils;

import net.mysgame.czfun.module.user.data.CzUser;
import net.mysgame.czfun.module.video.data.CzVideo;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.BeanUtilsBean2;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.plugin.com.TypeConverter;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by manatea on 2016/5/31.
 */
public class BeanWrapperUtils {
    public static Logger LOGGER = LoggerFactory.getLogger(BeanWrapperUtils.class);
    private static BeanUtilsBean beanUtilsBean = BeanUtilsBean.getInstance();

    /**
     * 将#{}占位符替换
     * @param sql
     * @param obj
     * @return
     */
    public static String nameParameterConvert(String sql, Object obj) {
        Pattern pattern = Pattern.compile("#\\{[a-zA-Z]+\\}");
        Matcher matcher = pattern.matcher(sql);
        StringBuffer rs = new StringBuffer();
        try {
            while (matcher.find()) {
                String variable = matcher.group();
                Pattern pattern1 = Pattern.compile("[a-zA-Z]+");
                Matcher matcher1 = pattern1.matcher(variable);
                if (matcher1.find()) {
                    String fieldName = matcher1.group();
                    Field field = null;
                    field = obj.getClass().getDeclaredField(fieldName);
                    field.setAccessible(true);
                    Object o = field.get(obj);
                    if (o == null) {
                        continue;
                    }
                    if (o instanceof Number) {
                        matcher.appendReplacement(rs, "'" + o + "'");
                    } else if(o instanceof Boolean){
                        String value = beanUtilsBean.getProperty(obj, field.getName());
                        matcher.appendReplacement(rs,value);
                    }
                    else {
                        String value = beanUtilsBean.getProperty(obj, field.getName());
                        matcher.appendReplacement(rs, "'" + value + "'");
                    }
                }
            }
            matcher.appendTail(rs);
            return rs.toString();
        } catch (NoSuchFieldException e) {
            LOGGER.error("BeanWrapperUtils failed to nameParameterConvert , error :", e);
        } catch (NoSuchMethodException e) {
            LOGGER.error("BeanWrapperUtils failed to nameParameterConvert , error :", e);
            ;
        } catch (IllegalAccessException e) {
            LOGGER.error("BeanWrapperUtils failed to nameParameterConvert , error :", e);
        } catch (InvocationTargetException e) {
            LOGGER.error("BeanWrapperUtils failed to nameParameterConvert , error :", e);
        }
        return null;
    }

  /*  public static void main(String[] args) {
        System.out.println(nameParameterConvert("REPLACE INTO cz_user(id,name,password,role,sex,tel,email)" +
                        " VALUES(#{id},#{name},#{password},#{role},#{sex},#{tel},#{email})",
                new CzUser(IdUtils.generateUserId("manatea"), "manatea", "123456", (byte) 4, false, "hhaa", "hqhq")));

    }*/
}
